<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2018-02-06
 * Time: 17:42
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\validate\CheckUser;
use app\api\model\User as UserModel;
use app\lib\exception\SuccessMessage;
use app\lib\exception\UserException;
use think\Db;
use think\Exception;
use app\api\service\Token as TokenService;

class User extends BaseController
{
    // 根据用户id获取用户信息
    public function getUserInfo($uid = null){
        if(isset($uid)){
            echo $uid;
        }else{
            echo "没有uid";
        }
    }
    // 保存小程序用户的信息
    public function setWechatUserInfo(){
        $validate = new CheckUser();
        $validate->goCheck();
        $uid = TokenService::getCurrentUid();
        $userInfoArray = $validate->getDataByRule(input('post.'));
        // $userInfoArray = $validate->input('post.');
        Db::startTrans();
        try{
            $user = UserModel::get($uid);
            $user->nickname = $userInfoArray['user_nickname'];
            $user->country = $userInfoArray['user_country'];
            $user->province = $userInfoArray['user_province'];
            $user->city = $userInfoArray['user_city'];
            $user->gender = $userInfoArray['user_gender'];
            $user->language = $userInfoArray['user_language'];
            $user->save();
            Db::commit();
            return json(new SuccessMessage([
                'code' => 200,
                'msg' => '用户数据更新保存成功'
            ]),200);
        }catch (Exception $exception){
            Db::rollback();
            throw $exception;
        }
    }
    
    // 每点击抽奖一次，数据库抽奖次数减一
    public function reduceLotteryCount(){
        $result = UserModel::reduceLotteryCount();
        return $result;
    }

}